Walkthrough 9-2: Route events based on conditions
In this walkthrough, you create a flow to route events to either the American, United, Delta, or get all airline flows based on the value of an airline query parameter. You will:
· Use a Choice router.
· Use DataWeave expressions to set the router paths.
· Route all flight requests through the router.
Starting file
If you did not complete the previous walkthrough, you can get a starting file here. This file is also located in the solutions folder of the student files ZIP located in the Course Resources.
Look at possible airline values specified in the API
1. Return to the apdev-flights-ws project in Anypoint Studio.
2. Return to mua-flights-api.raml in src/main/resources/api.
3. Locate the airline query parameter and its possible values.
Create a new flow
4. Return to implementation.xml.
5. Drag a Flow scope from the Mule Palette and drop it at the top of the canvas above all the other flows.
6. Change the name of the flow to getFlights.
7. Move the GET /flights HTTP Listener from getAllAirlineFlights to the source section of getFlights.
Add a Choice router
8. Drag a Choice flow control element from the Mule Palette and drop it in process section of getFlights.
9. Add three parallel Flow Reference components to the Choice router.
10. Add a Flow Reference component to the default branch of the router.
11. In the first Flow Reference properties view, set the flow name and display name to getAmericanFlights.
12. Set the flow names and display names for the other two Flow References to getUnitedFlights and getDeltaFlights.
13. For the Flow Reference in the default branch, set the flow name and display name to getAllAirlineFlights.
Store the airline query parameter in a variable
14. Add a Set Variable transformer before the Choice router.
15. In the Set Variable properties view, set the name and display name to airline.
16. Switch the value field to expression mode then set its value to a query parameter called airline.
message.attributes.queryParams.airline
Configure the Choice router
17. In the Choice router getAmericanFlights flow reference branch, click the When scope.
18. In the When properties view, switch the value field to expression mode, and then add an expression to check if the airline variable is equal to american.
vars.airline == "american"
19. Set a similar expression for the United route, routing to it when vars.airline is equal to united.
20. Set a similar expression for the Delta route, routing to it when vars.airline is equal to delta.
Route all requests through the router
21. Add a Flow Reference to the flow before the Choice router.
22. Set the flow name and display name to setCode.
23. In getAmericanFlights, delete the HTTP Listener and the setCode Flow Reference.
24. In getUnitedFlights, delete the HTTP Listener and the setCode Flow Reference.
25. In getDeltaFlights, delete the HTTP Listener and the setCode Flow Reference.
Return JSON from the flow
26. In the getFlights flow, add a Transform Message component after the Choice router.
27. Change its display name to [Flight] to JSON.
28. Add a Logger at the end of the flow.
29. In the Transform Message properties view, change the output type to application/json and set the expression to payload.
Debug the application
30. Add a breakpoint to the airline Set Variable transformer at the beginning of getFlights.
31. Add a breakpoint to the [Flight] to JSON Transform Message component after the Choice router.
32. Save the file to redeploy the project in debug mode.
33. In Advanced REST Client, make a request to http://localhost:8081/flights.
34. In the Mule Debugger, step through the application; you should see the Choice router pass the event to the default branch.
35. Resume to the Transform Message component after the Choice router; the payload should be an ArrayList of Flight objects
36. Step to the Logger; the payload should be JSON.
37. Step to the end of the application.
38. Return to Advanced REST Client; you should see American, United, and Delta flights to SFO (the default airport code).
39. Add an airline query parameter set to american and send the request: http://localhost:8081/flights?airline=american.
40. In the Mule Debugger, step through the rest of the application; you should see the event passed to getAmericanFlights and then back to getFlights.
41. Return to Advanced REST Client; you should see only American flights to SFO returned.
42. Change the airline to united and add a second query parameter code set to LAX: http://localhost:8081/flights?airline=united&code=LAX.
43. Send the request.
44. In the Mule Debugger, step through the application; the event should be routed to the United branch.
45. Return to Advanced REST Client; you should see only United flights to LAX are returned.
46. Change the airline to delta and the code to PDX: http://localhost:8081/flights?airline=delta&code=PDX.
47. Send the request.
48. In the Mule Debugger, step through the application; the event should be routed to the Delta branch.
49. Return to Advanced REST Client; you should see only Delta flights to PDX are returned.
Note: This JSON structure does not match that specified in the API: mua-flights-api.raml in src/main/resources/api. The data should, of course, be transformed so the response from the API matches this specification – but we are going to hold off doing that right now so that the scenario stays simpler for the error handling module (there is one less error to handle at the beginning).
Test the application with a destination that has no flights
50. Remove the airline parameter and set the code to FOO: http://localhost:8081/flights?code=FOO.
51. In the Mule Debugger, step through the application; you should see multiple errors.
52. Return to Advanced REST Client; you should get a 500 Server Response and an exception.